package aussortiert;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;

import org.jdom.JDOMException;

import com.j2docx.DocxHandlerException;
import com.j2docx.operations.DOCXHandler;
import com.j2docx.spray.BookmarkSprayer;


public class LesezeichenSprayer {
	
	
	
	
	
	public static void sprayText(String bookmarkName, String value) {
		try {
			// for extraction
			String docPath = "C:\\Users\\vond\\Desktop\\Berichtsheft.docx";
			String targetDirPath = "C:\\Users\\vond\\Desktop\\BerichtsheftTargetDir";

			// for archiving
			String extractedPath = "C:\\Users\\vond\\Desktop\\BerichtsheftTargetDir\\Berichtsheft";
			// Target xml file to be written.
			String xmlFilePath = "C:\\Users\\vond\\Desktop\\BerichtsheftTargetDir\\Berichtsheft\\word\\document.xml";

			// Bookmark name
//			String bookmarkName = "bmt";
			// Text to be sprayed
//			String value = "OMG!";

			String style = ""; //"font-family:Arial;font-size:20;font-weight:normal;font-color:#000000;italic:false";

			DOCXHandler docxHandler = new DOCXHandler();

			BookmarkSprayer bmSprayer = new BookmarkSprayer();

			// (Unzip)Extracting docx to the targeted directory path
			docxHandler.doExtract(docPath, targetDirPath);

			// Writing text on the pre-defined bookmark in the target file.
//			bmSprayer.sprayTextTargetFile(xmlFilePath, bookmarkName, value, style);
			bmSprayer.sprayText(extractedPath, bookmarkName, value, style);
			
			// (Zip)Archiving to get docx file
			docxHandler.doZip(extractedPath, "C:\\Users\\vond\\Desktop\\Berichtsheft2.docx");
			
		} catch (DocxHandlerException dhe) {
			System.out.println("dhe");
			dhe.printStackTrace();
		} catch (FileNotFoundException fnf) {
			System.out.println("fnf");
			fnf.printStackTrace();
		} catch (IOException ioe) {
			System.out.println("ioe");
			ioe.printStackTrace();
		} catch (JDOMException jdome) {
			System.out.println("jdome");
			jdome.printStackTrace();
		}
	}
}
